<template>
<!-- 更改生日弹窗 -->
  <div>
    <van-datetime-picker
       v-model="currentDate"
       type="date"
       title=""
       :min-date="minDate"
       :max-date="maxDate"
       @cancel="$emit('close')"
       @confirm="onBirthday"
    />
  </div>
</template>

<script>
import { updateUserName } from '@/api/user'
import { formatData } from '@/utils'
export default {
  props: {
    value: {
      type: String,
      requried: true
    }
  },
  data () {
    return {
      minDate: new Date(1800, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    }
  },
  methods: {

    async onBirthday () {
      const currentDate = formatData(this.currentDate, '-')
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true,
        duration: 0
      })
      try {
        await updateUserName({
        // 2.后端更新
          birthday: currentDate
        })
        // 3. 前端更新
        this.$emit('input', currentDate)
        // 4.提示信息
        this.$toast.success('更新成功')
        // 5.弹层信息
        this.$emit('close')
      } catch (err) {
        this.$toast.fail('修改失败')
      }
    }
  }
}
</script>

<style>

</style>
